package fr.bstevant.android.WifiNotify;

/*
 * Copyright (C) 2010 Bruno STEVANT 
 * <bruno.stevant@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 */

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.preference.PreferenceManager;



public class WifiNotifyReceiver extends BroadcastReceiver {
	WifiManager wm;
	NotificationManager nm;
	Context ctxt;
	private int YOURAPP_NOTIFICATION_ID = 20100114;
	
	public void onReceive(Context context, Intent intent) {
		nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		ctxt = context;
		
		// Discard if notification are disable
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
		Boolean notifEnabled = settings.getBoolean("chkNotifEnabled", false);
		if (!notifEnabled) return;
		
		if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
			WifiInfo wifiInfo = wm.getConnectionInfo();
			if (wifiInfo.getSSID() != null) {
				this.showNotification("SSID: " + wifiInfo.getSSID(), "Connected to SSID: " + wifiInfo.getSSID(), "Touch for more details...");
			} else {
				nm.cancel(YOURAPP_NOTIFICATION_ID);
			}
		}
	}
	
	private void showNotification(String tickerText, String contentTitle, String contentText) {
		// This is who should be launched if the user selects our notification. 
        Intent intent = new Intent(ctxt, WifiNotify.class);

        // This is who should be launched if the user selects the app icon in the notification.
        PendingIntent contentIntent = PendingIntent.getActivity(ctxt, 0, intent, 0);
        
        // choose the ticker text
        //String tickerText = showIconOnly ? null : this.getString(statusBarTextID);
        int statusBarIconID = R.drawable.icon;
        long when = System.currentTimeMillis();

        Notification notification =  new Notification(statusBarIconID, tickerText, when);

        notification.setLatestEventInfo(ctxt, contentTitle, contentText, contentIntent);

        nm.notify(
                   YOURAPP_NOTIFICATION_ID, // we use a string id because it is a unique
                                                      // number.  we use it later to cancel the
                                                      // notification
                   notification);
	}
}
